package com.platform.usercenter.controller;


import com.platform.common.core.constant.CacheConstants;
import com.platform.common.core.domain.entity.SysDept;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.domain.model.LoginUser;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.core.web.page.TableDataInfo;
import com.platform.common.redis.service.RedisService;
import com.platform.common.security.service.TokenService;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.ContactFrequent;
import com.platform.usercenter.domain.SysUserDept;
import com.platform.usercenter.domain.SysUserOnline;
import com.platform.usercenter.domain.query.SysUserDeptQuery;
import com.platform.usercenter.domain.vo.ContactVo;
import com.platform.usercenter.service.IContactFrequentService;
import com.platform.usercenter.service.ISysDeptService;
import com.platform.usercenter.service.ISysUserDeptService;
import com.platform.usercenter.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * 常用联系人通讯录Controller
 *
 * @author platform
 * @date 2023-10-08
 */
@RestController
@RequestMapping("/frequent")
public class ContactFrequentController extends BaseController {
	@Resource
	private IContactFrequentService contactFrequentService;

	@Resource
	private ISysUserDeptService sysUserDeptService;

	@Resource
	private ISysUserService userService;

	@Resource
	private RedisService redisService;


	@Resource
	private ISysDeptService sysDeptService;


	/**
	 * 查询常用联系人通讯录列表
	 */
	@GetMapping("/list")
	public AjaxResult list( ContactFrequent contactFrequent ) {
		//查询登录用户拥有的contactId集合
		LoginUser loginUser=SecurityUtils.getLoginUser( );
		Long userId=loginUser.getSysUser( ).getUserId( );
		ContactVo contactVo=new ContactVo( );
		BeanUtils.copyProperties( contactFrequent,contactVo );
		contactVo.setRelationUserId( userId );
		List< ContactFrequent > list=contactFrequentService.selectContactFrequentListByContact( contactVo );
		for(ContactFrequent frequent: list) {
			SysUser user=userService.selectUserById( frequent.getUserId( ) );
			if(StringUtils.isNotEmpty(  user.getAvatar() )){
				frequent.setAvatar( user.getAvatar() );
			}
		}
		return AjaxResult.success( list );
	}

	/**
	 * 导出常用联系人通讯录列表
	 */
	@PostMapping("/export")
	public void export( HttpServletResponse response,ContactFrequent contactFrequent ) {
		List< ContactFrequent > list=contactFrequentService.selectContactFrequentList( contactFrequent );
		ExcelUtil< ContactFrequent > util=new ExcelUtil< ContactFrequent >( ContactFrequent.class );
		util.exportExcel( response,list,"常用联系人通讯录数据" );
	}


	/**
	 * 新增常用联系人通讯录
	 */
	@PostMapping
	public AjaxResult add( @RequestBody ContactFrequent contactFrequent ) {
		return toAjax( contactFrequentService.insertContactFrequent( contactFrequent ) );
	}

	/**
	 * 修改常用联系人通讯录
	 */
	@PutMapping
	public AjaxResult edit( @RequestBody ContactFrequent contactFrequent ) {
		return toAjax( contactFrequentService.updateContactFrequent( contactFrequent ) );
	}

	/**
	 * 删除常用联系人通讯录
	 */
	@DeleteMapping("/{userIds}")
	public AjaxResult remove( @PathVariable Long[] userIds ) {
		return toAjax( contactFrequentService.deleteContactFrequentByContactIds( userIds ) );
	}


	/**
	 * 获取联系人详情
	 */
	@GetMapping("/detail")
	public AjaxResult getContactDetail( ContactFrequent contactFrequent ) {
		/**
		 * 获取当前登入用户部门信息
		 */
		SysUser user=userService.selectUserById( contactFrequent.getUserId( ) );
		SysUserDeptQuery query=new SysUserDeptQuery();
		query.setUserId( user.getUserId( ) );
		query.setDeptId( user.getDeptId( ) );//当前用户登入部门
		List< SysUserDept > sysUserDeptList=sysUserDeptService.selectSysUserDeptList( query );
		AjaxResult ajax=AjaxResult.success( user );
		HashMap< String, Boolean > onlineStatus=ifOnline( user.getUserId( ) );
		ajax.put( "roleGroup",userService.selectUserRoleGroup( user.getUserName( ) ) );
		ajax.put( "postGroup",userService.selectUserPostGroup( user.getUserName( ) ) );
		ajax.put( "userDept",CollectionUtils.isNotEmpty( sysUserDeptList )? sysUserDeptList.get( 0 ) : new ArrayList<>( ) );
		ajax.put( "onlineStatus",onlineStatus );
		return ajax;
	}

	/**
	 * 判断用户是否在线
	 */
	public HashMap< String, Boolean > ifOnline( Long userid ) {
		HashMap< String, Boolean > onlineMap=new HashMap<>( );
		Boolean pcOnline=false;
		Boolean appOnline=false;
		Boolean wchatOnline=false;
		Boolean watchOnline=false;
		Boolean vrOnline=false;

		//判断PC端是否在线
		Collection< String > keys=redisService.keys( CacheConstants.LOGIN_TOKEN_KEY+"*" );
		for(String key: keys) {
			LoginUser user=redisService.getCacheObject( key );
			Long userId=user.getSysUser( ).getUserId( );
			if(userid.compareTo( userId )==0) {
				pcOnline=true;
				break;
			}
		}

		//判断APP端是否在线
		Object user=redisService.getCacheObject( CacheConstants.APP_ONLINE_DIR+userid );
		if(user!=null) {
			appOnline=true;
		}


		//判断小程序端是否在线
		user=redisService.getCacheObject( CacheConstants.WCHAT_ONLINE_DIR+userid );
		if(user!=null) {
			wchatOnline=true;
		}

		//判断手表端是否在线
		user=redisService.getCacheObject( CacheConstants.WATCH_ONLINE_DIR+userid );
		if(user!=null) {
			watchOnline=true;
		}

		//判断眼镜端是否在线
		user=redisService.getCacheObject( CacheConstants.SPECTACLES_ONLINE_DIR+userid );
		if(user!=null) {
			vrOnline=true;
		}
		onlineMap.put( "pc_online",pcOnline );
		onlineMap.put( "app_online",appOnline );
		onlineMap.put( "wchat_online",wchatOnline );
		onlineMap.put( "watch_online",watchOnline );
		onlineMap.put( "spectacles_online",vrOnline );
		return onlineMap;
	}

	/**
	 * app小程序通讯录列表
	 */
	@GetMapping("/appContactList")
	public AjaxResult getappContactList( ContactVo contactVo ) {
		List< SysUser > sysUsers=userService.selectAllSysUser( new SysUser( ) );
		for(SysUser sysUser: sysUsers) {
			SysDept sysDept=sysDeptService.selectDeptById( sysUser.getDeptId( ) );
			sysUser.setDept( sysDept );
		}
		return AjaxResult.success( sysUsers );
	}

	/**
	 * 判断用户是否是当前的常用联系人
	 */
	@GetMapping("/ifContact")
	public AjaxResult ifContact( ContactFrequent contactFrequent ) {
		//查询登录用户拥有的contactId集合
		LoginUser loginUser=SecurityUtils.getLoginUser( );
		Long userId=loginUser.getSysUser( ).getUserId( );
		ContactVo contactVo=new ContactVo( );
		BeanUtils.copyProperties( contactFrequent,contactVo );
		contactVo.setRelationUserId( userId );
		contactVo.setUserId( contactFrequent.getUserId( ) );
		List< ContactFrequent > list=contactFrequentService.selectContactFrequentListByContact( contactVo );
		if(CollectionUtils.isNotEmpty( list )) {
			return AjaxResult.success( "true" );
		}
		return AjaxResult.success( "false" );
	}

}
